package com.ccarlos.mapper;

import com.ccarlos.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;

//@CacheNamespace(implementation = PerpetualCache.class) //开启二级缓存
@CacheNamespace(implementation = RedisCache.class) //开启二级缓存
public interface IUserMapper {

    //查询所有用户、同时查询每个用户关联的订单信息
    public List<User> findAll();

    //查询所有用户、同时查询每个用户关联的订单信息
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "orderList",column = "id",javaType = List.class,
                    many=@Many(select = "com.ccarlos.mapper.IOrderMapper.findOrderByUidByAnnotation"))
    })
    public List<User> findAllByAnnotation();

    //查询所有用户、同时查询每个用户关联的角色信息
    public List<User> findAllUserAndRole();

    //查询所有用户、同时查询每个用户关联的角色信息
    @Select("select * from user")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "roleList",column = "id",javaType = List.class,
                    many = @Many(select = "com.ccarlos.mapper.IRoleMapper.findRoleByUidByAnnotation"))
    })
    public List<User> findAllUserAndRoleByAnnotation();

    //添加用户
    @Insert("insert into user values(#{id},#{username})")
    public void addUserByAnnotation(User user);

    //更新用户
    @Update("update user set username = #{username} where id = #{id}")
    public void updateUserByAnnotation(User user);

    //查询用户
    @Select("select * from user")
    public List<User> selectUserByAnnotation();

    //删除用户
    @Delete("delete from user where id = #{id}")
    public void deleteUserByAnnotation(Integer id);


    //根据id查询用户
    @Options(useCache = true)
    @Select({"select * from user where id = #{id}"})
    public User findUserByIdByAnnotation(Integer id);


}
